#include "qlineedithost.h"
#include "qabstractproperty.h"
#include "qpropertyfactory.h"

#include <QLineEdit>

using namespace SHAREDLIB;

QLineEditHost::QLineEditHost(QObject *object):
    QWidgetHost(object)
{
    QAbstractProperty *pro;

    pro=QPropertyFactory::createProperty("ByteArray");
    if(pro!=NULL)
    {
        pro->setAttribute("name","text");
        pro->setAttribute("property_name",tr("Text"));
        pro->setAttribute("group","Attributes");
        addProperty(pro);
    }

    pro=QPropertyFactory::createProperty("Align");
    if(pro!=NULL)
    {
        pro->setAttribute("name","alignment");
        pro->setAttribute("property_name",tr("Alignment"));
        pro->setAttribute("group","Attributes");
        addProperty(pro);
    }


    pro=QPropertyFactory::createProperty("Bool");
    if(pro!=NULL)
    {
        pro->setAttribute("name","readOnly");
        pro->setAttribute("property_name",tr("ReadOnly"));
        pro->setAttribute("group","Attributes");
        addProperty(pro);
    }

    pro=QPropertyFactory::createProperty("Bool");
    if(pro!=NULL)
    {
        pro->setAttribute("name","frame");
        pro->setAttribute("property_name",tr("Frame"));
        pro->setAttribute("group","Attributes");
        addProperty(pro);
    }

    pro=QPropertyFactory::createProperty("Script");
    if(pro!=NULL)
    {
        pro->setAttribute("name","textChanged");
        pro->setAttribute("property_name",tr("TextChanged"));
        pro->setAttribute("group","Events");
        m_object->setProperty("textChanged","");
        addProperty(pro);
    }

    pro=QPropertyFactory::createProperty("Script");
    if(pro!=NULL)
    {
        pro->setAttribute("name","editFinish");
        pro->setAttribute("property_name",tr("EditFinish"));
        pro->setAttribute("group","Events");
        m_object->setProperty("editFinish","");
        addProperty(pro);
    }

    appendMemery("text");

    QLineEdit* e=(QLineEdit*)m_object;
    connect(e,SIGNAL(textChanged(QString)),this,SLOT(textChanged(QString)));
    connect(e,SIGNAL(editingFinished()),this,SLOT(editFinish()));
}

void QLineEditHost::setText(const QString text)
{
    setPropertyValue("text",text);
}

QString QLineEditHost::text()
{
    return getPropertyValue("text").toString();
}

void QLineEditHost::setAlignment(int alignment)
{
    setPropertyValue("alignment",alignment);
}

int QLineEditHost::alignment()
{
    return getPropertyValue("alignment").toInt();
}

void QLineEditHost::setReadOnly(bool readOnly)
{
    setPropertyValue("readOnly",readOnly);
}

bool QLineEditHost::readOnly()
{
    return getPropertyValue("readOnly").toBool();
}

void QLineEditHost::setFrame(bool frame)
{
    setPropertyValue("frame",frame);
}

bool QLineEditHost::frame()
{
    return getPropertyValue("frame").toBool();
}

void QLineEditHost::textChanged(const QString &text)
{
    QString code=getPropertyValue("textChanged").toString();
    if(code!="")
    {
        QMap<QString,QString> param;
        param.insert("text",text);
        emit_exec(code,param);
    }
}

void QLineEditHost::editFinish()
{
    QString code=getPropertyValue("editFinish").toString();
    if(code!="")
    {
        QMap<QString,QString> param;
        emit_exec(code,param);
    }
}
